#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
from openpyxl import load_workbook

url = "https://lishi.tianqi.com/hejin/202309.html"

data_frames = {}
def get_content(url, sheet):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
    wb_data = requests.get(url, headers=headers)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    data_list = soup.select('div.tian_three > ul > li > div.th200')
    data_list_elements = [element.text for i, element in enumerate(data_list)]
    print(data_list_elements)
    high_low_temperature_weather_elements = soup.select('div.tian_three > ul > li > div.th140')
    high_temperature_weather = [element.text for i, element in enumerate(high_low_temperature_weather_elements) if i % 2 == 0]
    high_temperatures = high_temperature_weather[::2]
    weather_temperatures = high_temperature_weather[1::2]

    low_temperature_weather = [element.text for i, element in enumerate(high_low_temperature_weather_elements) if
                                i % 2 == 1]
    low_temperature = low_temperature_weather[::2]
    print(high_temperatures)
    print(weather_temperatures)
    print(low_temperature)


    columns = ['日期', '最高温度', '最低温度', '天气']
    df = pd.DataFrame(columns=columns)
    data_to_append = []

    # # 使用 zip 函数遍历多个列表，并插入到 DataFrame 中
    for data_list, high,  low, weather in zip(data_list_elements, high_temperatures, low_temperature, weather_temperatures):
        data_to_append.append({'日期': data_list, '最高温度': high, '最低温度': low, '天气': weather})
    # 将要添加的数据转换为 DataFrame
    df_to_append = pd.DataFrame(data_to_append)
    # 追加数据到 DataFrame 中
    df = pd.concat([df, df_to_append], ignore_index=True)
    #添加数据到字典中
    data_frames[sheet] = df

#urls=[ "https://lishi.tianqi.com/hejin/2023{:02d}.html".format(number) for number in range(1, 13)]
for month in range(1, 13):
    formatted_month = '{:02d}'.format(month)
    single_url = "https://lishi.tianqi.com/hejin/2022" + formatted_month + ".html"
    print(single_url)
    get_content(single_url, formatted_month)
    #把字典中的数据以此写入表格中
    filename = "2022年河津天气.xlsx"
    with pd.ExcelWriter(filename, engine='openpyxl') as writer:
        for sheet_name, df in data_frames.items():
            df.to_excel(writer, sheet_name=sheet_name, index=False)




